﻿using Itech_Run.Helper.Configure;
using Itech_Run.Helper.Cryptography;
using Itech_Run.Sys.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace Itech_Run.Sys.EntityFrameworkCore.SeedData
{
    public static partial class SeedData
    {
        private static string Role_Admin = Guid.NewGuid().ToString();
        private static string User_Admin = Guid.NewGuid().ToString();
        private static string ApllicationCode = "Back";
        private static string Module_用户管理 = Guid.NewGuid().ToString();
        private static string Module_角色管理 = Guid.NewGuid().ToString();
        private static string Module_模块管理 = Guid.NewGuid().ToString();
        private static string Module_字典管理 = Guid.NewGuid().ToString();
        private static string Module_文章管理 = Guid.NewGuid().ToString();

        private static Sys_Module buildSysModule(string id, string name, int level, string? parentId, string? url, bool? isShowUI, int sort, bool isActive, string? apiController, string? assemblyName)
        {
            return new Sys_Module
            {
                Id = id,
                ApplicationCode = ApllicationCode,
                Name = name,
                Level = level,
                ParentId = parentId,
                Url = url,
                IsShowUI = isShowUI,
                Sort = sort,
                IsActive = isActive,
                ApiController = apiController,
                AssemblyName = assemblyName
            };
        }

        private static Sys_Button buildSysButton(string name, string code, int sort)
        {
            return new Sys_Button
            {
                Id = Guid.NewGuid().ToString(),
                Name = name,
                Code = code,
                Sort = sort
            };
        }

        private static Sys_Dictionary buildSysDictionary(int category, string codeType, string name, string code, string? value, int sort, string? remark, bool isActive, bool? isShow)
        {
            return new Sys_Dictionary
            {
                Id = Guid.NewGuid().ToString(),
                Category = category,
                CodeType = codeType,
                Name = name,
                Code = code,
                Value = value,
                Sort = sort,
                Remark = remark,
                IsActive = isActive,
                IsShow = isShow
            };
        }

        public static void CommonSeed(this ModelBuilder modelBuilder, IOptions<AesOptions> aesOptions)
        {
            modelBuilder.Entity<Sys_Role>().HasData(
                new Sys_Role { Id = Role_Admin, Name = "Admin", IsActive = true }
            );

            string password = "123456".AesEncypt(aesOptions.Value.Key, aesOptions.Value.IV);
            modelBuilder.Entity<Sys_User>().HasData(new Sys_User
            {
                Category = 0,
                Id = User_Admin,
                Name = "管理员",
                Nickname = "管理员1",
                UserName = "Admin",
                Password = password,
                IsActive = true,
                Sort = 1
            });

            modelBuilder.Entity<Sys_UserRole>().HasData(
                new Sys_UserRole
                {
                    Id = Guid.NewGuid().ToString(),
                    RoleId = Role_Admin,
                    UserId = User_Admin
                }
            );

            #region 模块
            List<Sys_Module> module_level1 = new List<Sys_Module>
            {
                buildSysModule(id: Module_用户管理, name: "用户管理", level: 1, parentId: "", url: "/user", isShowUI: true, sort: 1, isActive: true, apiController: "", assemblyName: ""),
                buildSysModule(id: Module_角色管理, name: "角色管理", level: 1, parentId: "", url: "/role", isShowUI: true, sort: 3, isActive: true, apiController: "", assemblyName: ""),
                buildSysModule(id: Module_模块管理, name: "模块管理", level: 1, parentId: "", url: "/module", isShowUI: true, sort: 4, isActive: true, apiController: "", assemblyName: ""),
                buildSysModule(id: Module_字典管理, name: "字典管理", level: 1, parentId: "", url: "/dictionary", isShowUI: true, sort: 5, isActive: true, apiController: "", assemblyName: ""),
                buildSysModule(id: Module_文章管理, name: "文章管理", level: 1, parentId: "", url: "/article", isShowUI: true, sort: 10, isActive: true, apiController: "", assemblyName: "")
            };
            modelBuilder.Entity<Sys_Module>().HasData(module_level1);

            List<Sys_Module> module_level2 = new List<Sys_Module>
            {
                buildSysModule(id: Guid.NewGuid().ToString(), name: "角色列表", level: 2, parentId: Module_角色管理, url: "list", isShowUI: true, sort: 1, isActive: true, apiController: "SysRole", assemblyName: "Itech_Run.Sys.HttpApi"),
                buildSysModule(id: Guid.NewGuid().ToString(), name: "用户列表", level: 2, parentId: Module_用户管理, url: "list", isShowUI: true, sort: 1, isActive: true, apiController: "SysUser", assemblyName: "Itech_Run.Sys.HttpApi"),

                buildSysModule(id: Guid.NewGuid().ToString(), name: "文章列表", level: 2, parentId: Module_文章管理, url: "list", isShowUI: true, sort: 1, isActive: true, apiController: "BlogInfo", assemblyName: "Itech_Run.Blog.HttpApi"),
                buildSysModule(id: Guid.NewGuid().ToString(), name: "创建文章", level: 2, parentId: Module_文章管理, url: "create", isShowUI: true, sort: 2, isActive: true, apiController: "BlogInfo", assemblyName: "Itech_Run.Blog.HttpApi"),
                buildSysModule(id: Guid.NewGuid().ToString(), name: "编辑文章", level: 2, parentId: Module_文章管理, url: "edit/:id", isShowUI: false, sort: 3, isActive: true, apiController: "BlogInfo", assemblyName: "Itech_Run.Blog.HttpApi"),

                buildSysModule(id: Guid.NewGuid().ToString(), name: "模块列表", level: 2, parentId: Module_模块管理, url: "list", isShowUI: true, sort: 1, isActive: true, apiController: "SysModule", assemblyName: "Itech_Run.Sys.HttpApi"),
                buildSysModule(id: Guid.NewGuid().ToString(), name: "字典列表", level: 2, parentId: Module_字典管理, url: "list", isShowUI: true, sort: 1, isActive: true, apiController: "SysDictionary", assemblyName: "Itech_Run.Sys.HttpApi")
            };
            modelBuilder.Entity<Sys_Module>().HasData(module_level2);

            List<Sys_Module> module_all = module_level1.Union(module_level2).ToList();
            #endregion

            List<Sys_Button> buttons = new List<Sys_Button> {
                buildSysButton("新增", "add", 1),
               buildSysButton("编辑", "edit", 2),
               buildSysButton("删除", "delete", 3),
               buildSysButton("保存", "save", 4)
            };
            modelBuilder.Entity<Sys_Button>().HasData(buttons);

            #region 角色模块：所有
            List<Sys_RoleModule> roleModules = new List<Sys_RoleModule>();
            module_all.ForEach(item =>
            {
                roleModules.Add(new Sys_RoleModule
                {
                    Id = Guid.NewGuid().ToString(),
                    ModuleId = item.Id,
                    RoleId = Role_Admin
                });
            });
            modelBuilder.Entity<Sys_RoleModule>().HasData(roleModules);
            #endregion

            #region 模块按钮：url=list对应所有按钮
            List<Sys_ModuleButton> moduleButtons = new List<Sys_ModuleButton>();
            module_level2.Where(p => p.Url == "list").ToList().ForEach(item =>
            {
                buttons.ForEach(itemBtn =>
                {
                    moduleButtons.Add(new Sys_ModuleButton
                    {
                        Id = Guid.NewGuid().ToString(),
                        ButtonId = itemBtn.Id,
                        ModuleId = item.Id
                    });
                });
            });
            modelBuilder.Entity<Sys_ModuleButton>().HasData(moduleButtons);
            #endregion

            #region 角色模块按钮，url=list对应所有按钮权限
            List<Sys_RoleModuleButton> roleModuleButtons = new List<Sys_RoleModuleButton>();
            roleModules.ForEach(item =>
            {
                buttons.ForEach(itemBtn =>
                {
                    roleModuleButtons.Add(new Sys_RoleModuleButton
                    {
                        Id = Guid.NewGuid().ToString(),
                        ButtonId = itemBtn.Id,
                        ModuleId = item.ModuleId,
                        RoleId = item.RoleId,
                        HasAuth = true
                    });
                });
            });
            modelBuilder.Entity<Sys_RoleModuleButton>().HasData(roleModuleButtons);
            #endregion

            modelBuilder.Entity<Sys_Dictionary>().HasData(
                buildSysDictionary(1, "1", "所属应用", "2", "", 1, "字典类别", true, false),
                buildSysDictionary(1, "1", "字典类型", "3", "", 2, "字典类别", true, false),
                buildSysDictionary(1, "2", "管理后台", "Back", "", 1, "所属应用", true, true),
                buildSysDictionary(1, "3", "普通", "1", "", 1, "字典类型", true, true),
                buildSysDictionary(1, "3", "附件", "2", "", 2, "字典类型", true, true)
           );
        }
    }
}
